package com.yangyouli.unity_demo_server.controller;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.yangyouli.unity_demo_server.annotation.ResponseClass;
import com.yangyouli.unity_demo_server.annotation.ResponseMethod;
import com.yangyouli.unity_demo_server.server.serializeModel.PlayerStatus;
import com.yangyouli.unity_demo_server.util.ThreadLocalHelper;

/**
 * 同步角色的位置旋转等状态到服务器
 * 
 * @author 24279
 *
 */
@ResponseClass("syncController")
public class SyncController {
	Logger log = Logger.getLogger(SyncController.class);

	/**
	 * 玩家发来的状态同步信息，转发给房间内的所有其他玩家
	 * 
	 * @param data
	 * @return
	 */
	@ResponseMethod("playerState")
	public String syncPlayerState(String data) {
		PlayerStatus playerStatus = JSONObject.parseObject(data, PlayerStatus.class);
		playerStatus.setUserId("" + ThreadLocalHelper.getClient().getUserId());
		ThreadLocalHelper.getClient().setPlayerStatus(playerStatus);
		return JSONObject.toJSONString(playerStatus);
	}

	/**
	 * 玩家捡起道具（比如枪支）
	 * 
	 * @param data
	 */
	@ResponseMethod("addItem")
	public String addItem(String data) {
		return String.valueOf(ThreadLocalHelper.getClient().getUserId()) + "-" + data;
	}

	/**
	 * 玩家丢弃道具（比如枪支）
	 * 
	 * @param data
	 * @return
	 */
	@ResponseMethod("removeItem")
	public String removeItem(String data) {
		return String.valueOf(ThreadLocalHelper.getClient().getUserId()) + "-" + data;
	}

	/**
	 * 玩家使用道具（比如枪支）
	 * 
	 * @param data
	 */
	@ResponseMethod("useItem")
	public String useItem(String data) {
		return String.valueOf(ThreadLocalHelper.getClient().getUserId()) + "-" + data;
	}
	
	/**
	 * 转发开始使用道具的操作，用于播放使用道具的动画
	 * @param data
	 * @return
	 */
	@ResponseMethod("startUseItem")
	public String startUseItem (String data) {
		return ThreadLocalHelper.getClient().getUserId() + "-" + data;
	}

	/**
	 * 返回玩家所在的房间的房主 id，没加入房间则返回 -1
	 * 
	 * @param data
	 * @return
	 */
	@ResponseMethod("isMaster")
	public String syncZombie(String data) {
		// 玩家在队伍中且房主的id等于当前用户的id，则是房主
		if (ThreadLocalHelper.getClient().getRoomObj() == null)
			return "-1";
		return "" + ThreadLocalHelper.getClient().getRoomObj().getMaster().getUserId();
	}

	@ResponseMethod("updateBullet")
	public String updateBullet(String data) {
		return ThreadLocalHelper.getClient().getUserId() + "-" + data;
	}
	
	/**
	 * 转发发射子弹的操作
	 * @param data
	 * @return
	 */
	@ResponseMethod("shoot")
	public String shoot (String data) {
		return ThreadLocalHelper.getClient().getUserId() + "-" + data;
	}
	
	/**
	 * 转发装填子弹的操作
	 * @param data
	 * @return
	 */
	@ResponseMethod("reload")
	public String reload (String data) {
		return ThreadLocalHelper.getClient().getUserId() + "-" + data;
	}
}
